﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
using System.Text;
using System.Drawing.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace MethodPicker
{
    public class MethodPicker : UITypeEditor
    {
        public MethodPicker()
            : base()
		{
		}

        private IWindowsFormsEditorService editorService = null;
        private MethodPickerEditorUI _assemblyMethodPickerEditorUI = null;

        public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            if (provider != null)
            {
                editorService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
                if (editorService != null)
                {
                    if (_assemblyMethodPickerEditorUI == null)
                        _assemblyMethodPickerEditorUI = new MethodPickerEditorUI();
                    _assemblyMethodPickerEditorUI.Start(editorService, value);
                    editorService.ShowDialog(_assemblyMethodPickerEditorUI);
                    value = _assemblyMethodPickerEditorUI.ReturnValue;
                    _assemblyMethodPickerEditorUI.End();
                }
            }

            return value;
        }

        public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.Modal;
        }
    }
}
